//
//  LoadMoreFootView.m
//  etionUI
//
//  Created by wu jingxing on 12-9-5.
//  Copyright (c) 2012年 GuangZhouXuanWu. All rights reserved.
//

#import "XUILoadMoreFootView.h"

#define EXTENDDISTANCE      20

@interface XUILoadMoreFootView ()
{
    UILabel *_textLabel;
    UIActivityIndicatorView *_ai;

    id <XUILoadMoreFootViewDelegate> _delegate;

    BOOL _bLoadMoreAction;
}
@end

@implementation XUILoadMoreFootView

- (id)initWithFrame:(CGRect)frame delegate:(id <XUILoadMoreFootViewDelegate>)delegate
{
    [super initWithFrame:frame];

    _delegate = delegate;

    _textLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.width - 150) / 2, (self.height - 20) / 2, 150, 20)];
    _textLabel.backgroundColor = [UIColor clearColor];
    _textLabel.font = [UIFont systemFontOfSize:14];
    _textLabel.text = @"上拉查看更多消息";
    _textLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_textLabel];

    _ai = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _ai.frame = CGRectMake(50, (self.height - _ai.height) / 2, _ai.width, _ai.height);
    [self addSubview:_ai];

    [self addTarget:self action:@selector(clickedLoadMoreFootView:) forControlEvents:UIControlEventTouchUpInside];

    _bLoadMoreAction = YES;

    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self loadMoreTip];
}

- (void)loadMoreTip
{
    UITableView *tableView = (UITableView *)self.superview;
    if([tableView isKindOfClass:[UITableView class]])
    {
        if(tableView.contentSize.height > tableView.height)
        {
            _textLabel.text = @"上拉查看更多消息";
        }
        else
        {
            _textLabel.text = @"点击加载更多";
        }
    }
}

- (void)responseLoadAction:(BOOL)bResponse
{
    _bLoadMoreAction = bResponse;
    if (_bLoadMoreAction == YES)
    {
        [self loadMoreTip];
    }
    else
    {
        _textLabel.text = @"无更多数据";
    }
}

- (void)loadMoreRefreshScrollViewDidEndDragging:(UIScrollView *)scrollview
{
    if (_bLoadMoreAction == NO)
        return;

    if (scrollview.height > scrollview.contentSize.height)
        return;
    if (scrollview.contentOffset.y + scrollview.height > scrollview.contentSize.height + EXTENDDISTANCE)
    {
        if ([_delegate loadMoreRefreshScrollViewIsLoading:scrollview] == NO)
            [_delegate loadMoreRefreshScrollViewDidEndDragging:scrollview];
    }
}

- (void)loadMoreRefreshScrollViewDidScroll:(UIScrollView *)scrollview
{
    if (_bLoadMoreAction == NO)
        return;

    if (scrollview.height > scrollview.contentSize.height)
        return;
    if ([_delegate loadMoreRefreshScrollViewIsLoading:scrollview] == NO)
    {
        if (scrollview.contentOffset.y + scrollview.height > scrollview.contentSize.height + EXTENDDISTANCE)
            _textLabel.text = @"加载更多";
        else
            _textLabel.text = @"上拉查看更多消息";
    }
}

- (void)loadMoreRefreshScrollViewDidLoading
{
    if (_bLoadMoreAction == NO)
        return;

    _textLabel.text = @"加载中";
    [_ai startAnimating];
}

- (void)loadMoreRefreshScrollViewDidFinishLoading
{
    if (_bLoadMoreAction == NO)
        return;

    //_textLabel.text = @"更多";
    
    [self loadMoreTip];
    
    [_ai stopAnimating];
}

- (void)clickedLoadMoreFootView:(id)sender
{
    if (_bLoadMoreAction == NO)
        return;

    if ([_delegate loadMoreRefreshScrollViewIsLoading:nil] == NO)
        [_delegate clickedLoadMoreFootView:self];
}

- (void)dealloc
{
    [_ai release_mm];
    [_textLabel release_mm];
    superdealloc_mm;
}

@end
